import axiosInstance from './axiosInstance'

/**
import axiosInstance from './axiosInstance.js';
/**
 * 获取首页数据
 * url: /
 * method: get
 * @returns 首页数据
 */
export async function getHomeData(): Promise<ResponseObject<HomeData>> {
    const response = await axiosInstance.get('/');
    return response as any;
}

export class UsersApi {

    /**
     * 用户登录
     * url: /account/login
     * method: post
     * @param username 用户名
     * @param password 密码
     * @returns 用户信息
     */
    static async login(username: string, password: string): Promise<ResponseObject<User>> {

        const response = await axiosInstance.post('/account/login', {
            username,
            password,
        });
        return response as any;

    }

    /**
     * 注销登录
     * url: /account/logout
     * method: post
     * @returns void
     */
    static async logout() {
        const response = await axiosInstance.post('/account/logout');
        return response as any;
    }

    /**
     * 注册新用户
     * url: /account/register
     * method: post
     * @param username 用户名
     * @param password 密码
     * @returns 用户信息
     */
    static async register(data: {
        username: string,
        password: string,
    }): Promise<ResponseObject<User>> {
        const response = await axiosInstance.post('/account/register', data)
        return response as any;
    }

    /**
     * 获取用户列表
     * url: /users 
     * method: get
     * @param page 页码
     * @param username 标题
     * @returns 用户列表
     */
    static async getUsers(username: string = '', page: number = 1): Promise<ResponsePage<User>> {
        const response = await axiosInstance.get('/users', {
            params: {
                page,
                username,
            }
        })
        return response as any;
    }

    /**
     * 获取用户首页数据
     * url: /account/index  
     * method: get
     * @returns 用户首页数据
     */
    static async getAccountIndex(): Promise<ResponseObject<any>> {
        const response = await axiosInstance.get('/account/index');
        return response as any;
    }
    /**
     * 获取当前用户信息
     * url: /account
     * method: get
     * @returns 用户信息
     */
    static async getCurrentUser(): Promise<ResponseObject<User>> {
        const response = await axiosInstance.get('/account');
        return response as any;
    }
    /**
     * 修改当前用户信息
     * url: /account
     * method: put
     * @param username 用户名
     * @param password 密码
     * @param description 描述
     * @returns 用户信息
     */
    static async updateCurrentUser(user: Partial<User>): Promise<ResponseObject<User>> {
        const response = await axiosInstance.put('/account', user)
        return response as any;
    }

}
export class TodosApi {


    /**
     * 获取所有的待办事项
     * @param page 页码
     * @param title 标题
     * @param completed 是否完成
     * @returns 待办事项列表
     */
    static async getTodos(title: string = '', completed: number | undefined = undefined, page: number = 1): Promise<ResponsePage<Todo>> {
        let res = await axiosInstance.get('/todos', {
            params: {
                page,
                title,
                completed
            }
        })
        return res as any
    }
    
    /**
     * 获取当前用户的待办事项
     * @param page 页码
     * @param title 标题
     * @param completed 是否完成
     * @returns 待办事项列表
     */
    static async getAccountTodos(title: string = '', completed: number | undefined = undefined, page: number = 1): Promise<ResponsePage<Todo>> {
        let res = await axiosInstance.get('/account/todos', {
            params: {
                page,
                title,
                completed
            }
        })
        return res as any
    }
    /**
     * 获取单个待办事项
     * @param id 待办事项id
     * @returns 待办事项
     */
    static async getTodo(id: number):Promise<ResponseObject<Todo>>{
        let res = await axiosInstance.get(`/todos/${id}`)
        return res as any
    }
    /**
     * 创建待办事项
     * @param title 标题
     * @param completed 是否完成
     * @param description 描述
     * @returns 待办事项
     */
    static async createTodo(title: string, description: string = '', completed: boolean = false) {
        let res = await axiosInstance.post('/todos', {
            title,
            completed,
            description
        })
        return res as any
    }
    /**
     * 更新待办事项
     * @param id 待办事项id
     * @param title 标题
     * @param completed 是否完成
     * @param description 描述
     * @returns 待办事项
     */
    static async updateTodo(id: number, title: string, description: string = '', completed: boolean = false) {
        let res = await axiosInstance.put(`/todos/${id}`, {
            title,
            completed,
            description
        })
        return res as any
    }
    /**
     * 删除待办事项
     * @param id 待办事项id
     * @returns 待办事项
     */
    static async deleteTodo(id: number) {
        let res = await axiosInstance.delete(`/todos/${id}`)
        return res as any
    }

}